<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.transcargo.data.CountriesData" %>
<%@ page import="com.transcargo.geo.Country" %>
<%@ page import="com.transcargo.geo.Region" %>
<%@ page import="com.transcargo.geo.City" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.transcargo.users.UserNotFoundException" %>
<%@include file="header.jsf"%>
<%@include file="session.jsf"%>
<%@include file="resolve_country.jsf"%>
<%
	Date date = new Date();
	Country[] countries = CountriesData.getCountries(dbase);
	
	String cid_from = request.getParameter("cid_from");
	String rid_from = request.getParameter("rid_from");
	String ctid_from = request.getParameter("ctid_from");
	String cid_to = request.getParameter("cid_to");
	String rid_to = request.getParameter("rid_to");
	String ctid_to = request.getParameter("ctid_to");
	String permanent = request.getParameter("permanent");
	String tir = request.getParameter("tir");
	String adr = request.getParameter("adr");
	String urgent = request.getParameter("urgent");
	String passing = request.getParameter("passing");
	String bid = request.getParameter("bid");
    String uid = request.getParameter("uid");
	Boolean immediateSearch = false;
	if (cid_from != null ||
			cid_to != null ||
			permanent != null ||
			tir != null ||
			adr != null ||
			urgent != null ||
			passing != null ||
			bid != null) immediateSearch = true;
			

%>
<html>
  	<head><title>TransCargo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="ajaxrequest.js"></script>
	<script type="text/javascript" src="chooseareas.js"></script>
    <script type="text/javascript" src="forms.js"></script>
	<link type="text/css" href="transcargo.css" rel="stylesheet"/>

<script type="text/javascript">
var xslDoc = new Array();
var xslDocNames = new Array();
xslDocNames[0] = 'transform/freight.xslt';
xslDocNames[1] = 'empty.xslt';
if (window.DOMParser == null) {
  xslDoc[0]=new ActiveXObject("Microsoft.XMLDOM");
  xslDoc[0].async="false";
  xslDoc[1]=new ActiveXObject("Microsoft.XMLDOM");
  xslDoc[1].async="false";
}

var num_countries=<%=countries.length%>;
var Countries=new Array();
var CountryValues=new Array();
<%
	out.println("Countries[0]='*';");
	out.println("CountryValues[0]='0';");
    for (int i = 0; i < countries.length; i++) {
        Country country = countries[i];
        out.println("Countries[" + (i+1) + "]='" + country.getName() + "';");
        out.println("CountryValues[" + (i+1) + "]='" + country.getId() + "';");
    }
%>
var cid = <%=country_id%>;

</script>
  </head>
  <body>
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr><td colspan="2">
			<%@include file="page_header.jsf"%>
        </td></tr>
        <tr><td width="80%" align="center">
            <table width="100%" cellpadding="0" cellspacing="0">
	            <tr><td align="center"><h1>ТрансКарго</h1></td></tr>
            <form name="sform" id="sform">
				<table border="0" cellpadding="0" cellspacing="0" width="800">
	            <tr><th align="center" colspan="2">Поиск транспорта</th></tr>
                <tr class="tdFormSearch"><td>Место загрузки:</td><td align="left"><input name="from" id="from" size="20" onfocus="toggleFrom(true)" onblur="toggleFrom(false)" onChange="changeFrom()" onkeyup="changeFrom()">
						(<a id="FRegionPlace">Любое</a>)
						<input type="hidden" name="regfrom" id="regFrom"><br>
						<div id="FromPlaceHolder" style="position: absolute;visibility:hidden;z-index: 999;"></div></td></tr>
                <tr class="tdFormSearch"><td>Место выгрузки:</td><td align="left"><input name="to" id="to" size="20" onfocus="toggleTo(true)" onblur="toggleTo(false)" onChange="changeTo()" onkeyup="changeTo()">
						(<a id="TRegionPlace">Любое</a>)
						<input type="hidden" name="regto" id="regTo"><br>
						<div id="ToPlaceHolder" style="position: absolute;visibility:hidden;z-index: 999;"></div></td></tr>
				    <tr><td class="tdFormSearch">Тип транспорта:</td><td><select name="cargo_type" id="cargo_type" class="selNormal">
			        <option value="0" selected>любой</option>
					<%
					        iter = null;
					        list = dbase.execute("SELECT id, name from transport_type");
					        if (list != null) iter = list.iterator();
                            int i = 0;
                            while (iter.hasNext()) {
                                String[] str = iter.next();
                                %><option value="<%=str[0]%>"><%=str[1]%></option><%
                                i++;
                            }
					%>
				    </select></td></tr>
				    <tr><td class="tdFormSearch">Дата c:</td><td><input name="date_from" id="date_from" value="<%=date.getDate()%>.<%=date.getMonth()+1%>.<%=date.getYear()+1900%>" size="11" onfocus="gfPop.fPopCalendar(document.getElementById('date_from'));return false;"> по <input name="date_to" id="date_to" value="<%=date.getDate()%>.<%=date.getMonth()+1%>.<%=date.getYear()+1900%>" size="11" onfocus="gfPop.fPopCalendar(document.getElementById('date_to'));return false;"></td></tr>
				    <tr valign="middle"><td class="tdFormSearch">Тоннаж:</td><td><input size="4" name="weight_from" value="0">-<input size="4" name="weight_to" value="20"> т.</td></tr>
				    <tr valign="middle"><td class="tdFormSearch">Объем:</td><td><input size="4" name="volume_from" value="0">-<input size="4" name="volume_to" value="82"> м<sup>3</sup></td></tr>
				    <tr><td class="tdFormSearch">Тип загрузки:</td><td align="left">
						<%
						        iter = null;
						        list = dbase.execute("SELECT id, name from transport_loading");
						        if (list != null) iter = list.iterator();
					            while (iter.hasNext()) {
					                String[] str = iter.next();
					                %><input type="checkbox" name="load<%=str[0]%>"><%=str[1]%><br><%
					            }
						%>
					</td></tr>
					<tr><td class="tdFormSearch">TIR:</td><td><input type="checkbox" name="tir"> ADR:<input type="checkbox" name="adr"></td></tr>
					<tr><td class="tdFormSearch">Срочный:</td><td><input type="checkbox" name="urgent"<%=(urgent!=null && urgent.equals("on"))?" checked":"" %>> Попутный: <input type="checkbox" name="passing"<%=(passing!=null && passing.equals("on"))?" checked":"" %>></td></tr>
					<tr><td class="tdFormSearch">Со ставкой:</td><td><input type="checkbox" name="bid"<%=(bid!=null && bid.equals("on"))?" checked":"" %>> Постоянный: <input type="checkbox" name="permanent"<%=(permanent!=null && permanent.equals("on"))?" checked":"" %>></td></tr>
<% if (uid !=null && uid.length() > 0) {
	int Uid = Integer.parseInt(uid);
	try {
		User company = new User(Uid, dbase);
%>					<tr><td class="tdFormSearch">Организация:</td><td><%=company.getName() %><input type="hidden" name="uid" value="<%=Uid %>"></td></tr>
<%
	} catch (UserNotFoundException e) {
		Uid = 0;
		uid = "";
	}
}
%>
				    <tr><td colspan="2" align="center"><input type="submit" id="btn1" value="Найти" onclick="submitSearch();return false;"></td></tr>
        	</table>
			</form>
			<iframe width=174 height=189 name="gToday:cal:none" id="gToday:cal:none" src="calendar/cal.htm" scrolling="no" frameborder="0" style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;"></iframe>
        </td><td valign="top"><%@include file="login.jsf"%><br><%@include file="quicksearch.jsf"%></td></tr>
        <tr><td colspan="3" align="left"><div id="MsgArea"></div></td></tr>
    </table>
<script type="text/javascript">
	var ef = document.getElementById('FromPlaceHolder');
	var et = document.getElementById('ToPlaceHolder');
	<% 
	if (cid_from != null && cid_from.length() > 0) {
		int cid = Integer.parseInt(cid_from);
		int rid = 0;
		int ctid = 0;
		if (rid_from != null && rid_from.length() > 0) rid = Integer.parseInt(rid_from);
		if (ctid_from != null && ctid_from.length() > 0) ctid = Integer.parseInt(ctid_from);
		out.println("	AddSearchArea('From',ef," + cid + "," + rid + "," + ctid + ");");
		out.println("	document.getElementById('regFrom').value='"+ cid + "-" + rid + "-" + ctid + "';");
	} else out.println("	AddSearchArea('From',ef);");
	if (cid_to != null && cid_to.length() > 0) {
		int cid = Integer.parseInt(cid_to);
		int rid = 0;
		int ctid = 0;
		if (rid_to != null && rid_to.length() > 0) rid = Integer.parseInt(rid_to);
		if (ctid_to != null && ctid_to.length() > 0) ctid = Integer.parseInt(ctid_to);
		out.println("	AddSearchArea('To',et," + cid + "," + rid + "," + ctid + ");");
		out.println("	document.getElementById('regTo').value='"+ cid + "-" + rid + "-" + ctid + "';");
		
	} else out.println("	AddSearchArea('To',et);");
    if (immediateSearch) out.println("	submitSearch(1);"); %>
</script>
  </body>
</html>
